package cn.edu.neu.studentmsg_java.cons;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

//异常枚举类
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum State {
    SUCCESS(20000, "请求成功"),
    FAILED(20404, "请求失败"),
    NO_AUTH(20401, StateConstants.NO_AUTH),
    DB_SAVE_ERROR(20405, StateConstants.DB_SAVE_ERROR),
    DB_UPDATE_ERROR(20406, StateConstants.DB_UPDATE_ERROR),
    DB_DELETE_ERROR(20407, StateConstants.DB_DELETE_ERROR),
    DB_DELETE_NOT_ALLOW(20408, StateConstants.DB_DELETE_NOT_ALLOW),
    DB_QUERY_ERROR(20409, StateConstants.DB_QUERY_ERROR),
    DB_QUERY_EMPTY(20410, StateConstants.DB_QUERY_EMPTY),
    MUST_FIELD_NON_EXISTENT(20410, StateConstants.MUST_FIELD_NON_EXISTENT),
    MUST_FIELD_VALUE_MISTAKE(20411, StateConstants.MUST_FIELD_VALUE_MISTAKE),


    /*
        管理员模块自定义响应码：20101-20150
     */
    SETTINGS_USER_LOGIN_SUCCESS(20100, StateConstants.SETTINGS_USER_LOGIN_SUCCESS),
    SETTINGS_USER_LOGIN_ACT_OR_LOGIN_PWD_ERROR(20101, StateConstants.SETTINGS_USER_LOGIN_ACT_OR_LOGIN_PWD_ERROR),
    SETTINGS_USER_ACCOUNT_EXPIRED(20102, StateConstants.SETTINGS_USER_ACCOUNT_EXPIRED),
    SETTINGS_USER_ACCOUNT_LOCKED(20103, StateConstants.SETTINGS_USER_ACCOUNT_LOCKED),
    SETTINGS_USER_IP_NOT_ALLOW(20104, StateConstants.SETTINGS_USER_IP_NOT_ALLOW);

    private Integer code;
    private String msg;

    //根据错误信息获取code码
    public static Integer getCodeByMsg(String msg) {
        //遍历枚举值
        for (State state : State.values()) {
            if (state.msg.equals(msg))
                return state.code;
        }
        return null;
    }

}
